﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Arthus
{
    class Razno
    {
        public void EnableDisableDugmiceIzmeniIBrisiUodnosuSelektovanIliNeRed(DataGridView dataGridViewUniverzalni, Button Obrisi, Button Izmeni)
        { //metoda koja disabluje ili enabluje dugmice u odnosu na to da li je slektovan red u datagridview ili ne
            if (dataGridViewUniverzalni.SelectedRows.Count > 0)
            {
                //ukoliko je selektovan red
                Obrisi.Enabled = true;
                Izmeni.Enabled = true;
            }
            else
            {
                //ukoliko nije selektovan red
                Obrisi.Enabled = false;
                Izmeni.Enabled = false;
            }
        } //metoda koja enable-uje ili disable-uje pojedine kontroleu zavisnosti od toga da li je selektovan red u datagridview ili ne

        public static string IzbrisiPojedineKaraktereIzStringa(string str) //konvertuje string tako da su prihvaceni samo sledeci karakteri: A-Z0-9
        {
            return Regex.Replace(str, "[^A-Z0-9]+", "", RegexOptions.Compiled);
        }
        public static string IzbrisiPojedineKaraktereIzStringa2(string str) //konvertuje string tako da su prihvaceni samo sledeci karakteri: 0-9 % (i space)
        {
            return Regex.Replace(str, "[^0-9 %]+", "", RegexOptions.Compiled);
        }
        public static string IzbrisiPojedineKaraktereIzStringa3(string str) //konvertuje string tako da su prihvaceni samo brojevi i tacka
        {
            return Regex.Replace(str, "[^0-9.]+", "", RegexOptions.Compiled);
        }
        public static void DozvoliUnosSamoBrojevaIJedneTacke(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar))
                    && (e.KeyChar != '.'))
                e.Handled = true;

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                e.Handled = true;

            // only allow minus sign at the beginning
            /*if (e.KeyChar == '-' && (sender as TextBox).Text.Length > 0)
                e.Handled = true;*/
        }
        public static void DozvoliUnosSamoBrojevaIJedneTackeIMinusa(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar))
                        && (e.KeyChar != '.') && (e.KeyChar != '-'))
                    e.Handled = true;

                // only allow one decimal point
                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
                    e.Handled = true;
                //onyly one minus
                if (e.KeyChar == '-' && (sender as TextBox).Text.IndexOf('-') > -1)
                    e.Handled = true;
                // only allow minus sign at the beginning
                /*if (e.KeyChar == '-' && (sender as TextBox).Text.Length > 0)
                    e.Handled = true;*/
            }
            catch (FormatException fe) {
                MessageBox.Show("Minus mora biti na početku! \n\n" + fe.ToString());
            }
        }
        public static void DozvoliMinusSamoNaPocetku(TextBox textbox)
        {
            if (textbox.Text.Trim().Length > 0)
            {
                if (textbox.Text.Contains("-") && (textbox.Text.Substring(0, 1) != "-" || textbox.Text.Split('-').Length > 2))
                {
                    bool headingDash = false;
                    if (textbox.Text.Substring(0, 1) == "-")
                    {
                        headingDash = true;
                    }
                    textbox.Text = textbox.Text.Replace("-", "");
                    if (headingDash)
                    {
                        textbox.Text = "-" + textbox.Text;
                    }
                }
            }
        }
        public static void DozvoliSamoBrojeve(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar)))
                e.Handled = true;
        }

        public static void comboKeyPressed(ComboBox combo)
        {
            combo.DroppedDown = true;
            object[] originalList = (object[])combo.Tag;
            if (originalList == null)
            {
                // backup original list
                originalList = new object[combo.Items.Count];
                combo.Items.CopyTo(originalList, 0);
                combo.Tag = originalList;
            }

            // prepare list of matching items
            string s = combo.Text.ToLower();
            IEnumerable<object> newList = originalList;
            if (s.Length > 0)
            {
                newList = originalList.Where(item => item.ToString().ToLower().Contains(s));
            }

            // clear list (loop through it, otherwise the cursor would move to the beginning of the textbox...)
            while (combo.Items.Count > 0)
            {
                combo.Items.RemoveAt(0);
            }
            // re-set list
            combo.Items.AddRange(newList.ToArray());
        }

        public static String IseciStringDoIzabranogChara(String s, char Char)
        {
            int l = s.IndexOf(Char);
            if (l > 0)
            {
                return s.Substring(0, l);
            }
            return "";
        }
    }
}
